package main

import (
	"alarm-flow/config"
	"alarm-flow/datasource"
	"alarm-flow/models"
	"alarm-flow/utils"
	"log/slog"
	"os"
)

var err error
var globalConfig *config.Config

func init() {
	globalConfig, err = config.New("")
	if err != nil {
		panic(err)
	}

	err = datasource.InitPg(globalConfig.Pg)
	if err != nil {
		panic(err)
	}

	log := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
		//AddSource:   true,
		Level: utils.SlogLevel(globalConfig.LogLevel),
	}))

	slog.SetDefault(log)
}

func main() {
	_, _ = datasource.PgEngine.Exec("create schema alarm_flow;")
	tables := []interface{}{
		new(models.AlarmConfig), new(models.AlarmCondition),
		new(models.PushConfig), new(models.PushRecord),
		new(models.AlarmRecordCategory), new(models.AlarmRecord),
	}

	err = datasource.PgEngine.Sync(tables...)
	if err != nil {
		panic(err)
	}

	PreData()
}

func PreData() {
	CpuAlarmConfig := &models.AlarmConfig{
		CollectItemId:   1,
		Measurement:     "host_cpu_usage",
		Field:           "used",
		Name:            "CPU",
		Enable:          true,
		Description:     "test",
		AlarmWindowSize: 15,
	}

	_, err = datasource.PgEngine.InsertOne(CpuAlarmConfig)

	condition := &models.AlarmCondition{AlarmConfigId: CpuAlarmConfig.Id, Level: "Fetal", Operator: ">=", Value: "30"}
	_, err = datasource.PgEngine.InsertOne(condition)
}
